import Https from "@/utils/Https";
import { ActionType, ProTable } from "@ant-design/pro-components";
import React, { useState, useRef, FC } from "react";
import { Button, Drawer, message, Space } from "antd";
import { calcColumns, calcColumns1 } from "./interface";
import Https2 from "@/utils/Https2";
const MessageSet: FC<any> = () => {
  const [loading, setLoading] = useState(false);
  const actionRef = useRef<ActionType>();
  const [open, setOpen] = useState(false);
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  const [conetId, setConetId] = useState("");
  //获取消息设置列表
  const getList = async (obj) => {
    setLoading(true);
    let records = [];
    let total = 0;
    let res = await Https.get("/messageTenantRelation/list", obj, true);
    setLoading(false);
    if (res.code == 200) {
      records = res.data.records;
      total = res.data.total;
    }
    return {
      success: true,
      data: records,
      total: total,
    };
  };
  //更新状态
  const toggleStatus = async (id) => {
    setLoading(true);
    let res = await Https.put(
      `/messageTenantRelation/updateStatus/${id}`,
      {},
      true
    );
    setLoading(false);
    if (res.code == 200) {
      message.success("修改成功");
      actionRef.current && actionRef.current.reload();
    } else {
      message.error(res.msg);
    }
  };
  //获取关联人数列表
  const getUserList = async (obj) => {
    setLoading(true);
    let records = [];
    let total = 0;
    let res = await Https2.post("/system/user/list", obj, true);
    setLoading(false);
    if (res.code == 200) {
      records = res.data.records;
      total = res.data.total;
    }
    return {
      success: true,
      data: records,
      total: total,
    };
  };
  //获取关联用户
  const getConetList = async (id) => {
    let res = await Https.get(
      "/messageTenantRelation/getUserIdList",
      { id },
      true
    );
    if (res.code == 200 && res.data) {
      setSelectedRowKeys(res.data.records);
    } else {
      setSelectedRowKeys([]);
    }
  };
  const operate = [
    {
      title: "操作",
      dataIndex: "operate",
      align: "center",
      fixed: "right",
      width: 200,
      hideInSearch: true,
      render: (_, record) => {
        return (
          <>
            <Button
              type="link"
              disabled={record.messageStatus == 3 || record.messageStatus == 4}
              onClick={() => {
                toggleStatus(record.id);
              }}
            >
              {record.status == 0 ? "禁用" : "启用"}
            </Button>
            <Button
              type="link"
              onClick={() => {
                setOpen(true);
                setSelectedRowKeys([]);
                setConetId(record.id);
                getConetList(record.id);
              }}
            >
              联系人
            </Button>
          </>
        );
      },
    },
  ];
  //获取选中的列
  const rowSelectChange = (selectedRowKeys, selectedRows, info) => {
    setSelectedRowKeys(selectedRowKeys);
  };
  //保存联系人
  const submitForm = async () => {
    if (selectedRowKeys.length == 0) {
      message.error("至少关联一个用户");
      return;
    }
    let params = {
      userIds: selectedRowKeys,
      id: conetId,
    };
    setLoading(true);
    let res = await Https.post(
      "/messageTenantRelation/createUserRelation",
      params,
      true
    );
    setLoading(false);
    if (res.code == 200) {
      message.success("关联成功");
      setOpen(false);
    } else {
      message.error(res.msg);
    }
  };
  return (
    <div key="Course">
      <ProTable
        columns={calcColumns({ operate })}
        rowKey="id"
        actionRef={actionRef}
        loading={loading}
        pagination={{
          showSizeChanger: true,
        }}
        request={async (params) => {
          return await getList(params);
        }}
      />
      <Drawer
        open={open}
        title="联系人"
        forceRender
        onClose={() => setOpen(false)}
        width="60%"
        extra={
          <Space>
            <Button
              onClick={() => {
                setOpen(false);
              }}
            >
              取消
            </Button>
            <Button onClick={submitForm} loading={loading} type="primary">
              确定
            </Button>
          </Space>
        }
      >
        {open && (
          <ProTable
            columns={calcColumns1}
            rowKey="userId"
            actionRef={actionRef}
            loading={loading}
            rowSelection={{
              onChange: rowSelectChange,
              selectedRowKeys,
            }}
            pagination={{
              showSizeChanger: true,
            }}
            request={async (params) => {
              return await getUserList(params);
            }}
          />
        )}
      </Drawer>
    </div>
  );
};
export default MessageSet;
